package com.github.jinatonic.confetti.sample.slice;

import com.github.jinatonic.confetti.ConfettiManager;
import com.github.jinatonic.confetti.ConfettiSource;
import com.github.jinatonic.confetti.ConfettoGenerator;
import com.github.jinatonic.confetti.confetto.BitmapConfetto;
import com.github.jinatonic.confetti.confetto.Confetto;
import com.github.jinatonic.confetti.sample.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.util.Random;

public class FallingConfettiWithListenerAbilitySlice extends AbstractAbilitySlice
        implements ConfettoGenerator, ConfettiManager.ConfettiAnimationListener {
    private Text numConfettiTxt;
    private int numConfettiOnScreen;

    private int size;
    private int velocitySlow, velocityNormal;
    private PixelMap bitmap;

    @Override
    protected void onStart(Intent savedInstanceState) {
        super.onStart(savedInstanceState);
        super.setUIContent(getLayoutRes());

        numConfettiTxt = (Text) findComponentById(ResourceTable.Id_num_confetti_txt);

        size = AttrHelper.vp2px(6,getContext());
        velocitySlow = AttrHelper.vp2px(50,getContext());
        velocityNormal = AttrHelper.vp2px(100,getContext());

        ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
        ImageSource source = null;
        try {
            source = ImageSource.create(getResourceManager().getResource(ResourceTable.Media_snowflake),sourceOptions);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        int s = AttrHelper.vp2px(30,getContext());
        decodingOptions.desiredSize = new Size(s,s);
        bitmap = source.createPixelmap(decodingOptions);
    }
    protected int getLayoutRes() {
        return ResourceTable.Layout_ability_confetti_with_listener;
    }
//    @Override
//    protected int getLayoutRes() {
//        return R.layout.activity_confetti_with_listener;
//    }

    @Override
    protected ConfettiManager generateOnce() {
        return getConfettiManager().setNumInitialCount(20)
                .setEmissionDuration(0)
                .setConfettiAnimationListener(this)
                .animate();
    }

    @Override
    protected ConfettiManager generateStream() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(3000)
                .setEmissionRate(20)
                .setConfettiAnimationListener(this)
                .animate();
    }

    @Override
    protected ConfettiManager generateInfinite() {
        return getConfettiManager().setNumInitialCount(0)
                .setEmissionDuration(ConfettiManager.INFINITE_DURATION)
                .setEmissionRate(20)
                .setConfettiAnimationListener(this)
                .animate();
    }

    private ConfettiManager getConfettiManager() {
        final ConfettiSource source = new ConfettiSource(0, -size, container.getWidth(), -size);
        return new ConfettiManager(this, this, source, container)
                .setVelocityX(0, velocitySlow)
                .setVelocityY(velocityNormal, velocitySlow)
                .setRotationalVelocity(180, 90)
                .setTouchEnabled(true);
    }

    @Override
    public Confetto generateConfetto(Random random) {
        return new BitmapConfetto(bitmap);
    }

    @Override
    public void onAnimationStart(ConfettiManager confettiManager) {

        ToastDialog dialog = new ToastDialog(getContext());
        dialog.setText("Starting confetti animation").setDuration(1500).show();
    }

    @Override
    public void onAnimationEnd(ConfettiManager confettiManager) {
        numConfettiOnScreen = 0;
        updateNumConfettiTxt();
        ToastDialog dialog = new ToastDialog(getContext());
        dialog.setText("Ending confetti animation").setDuration(1500).show();
    }

    @Override
    public void onConfettoEnter(Confetto confetto) {
        numConfettiOnScreen++;
        updateNumConfettiTxt();
    }

    @Override
    public void onConfettoExit(Confetto confetto) {
        numConfettiOnScreen--;
        updateNumConfettiTxt();
    }

    private void updateNumConfettiTxt() {


        try {
            numConfettiTxt.setText(getResourceManager().getElement(ResourceTable.String_num_confetti_desc).getString(numConfettiOnScreen));
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
    }
}
